#include "TT.h"

TT::TT(vector<Node*> nodesVec) {

	// initializing _the table
	vector<Node*>::iterator it;
	for (it = nodesVec.begin(); it < nodesVec.end(); it++) {
		entry *e = new entry();
		e->_priority = 0;
		e->_quantaTotal = 0;
		e->_quantaCurrentStage = 0;
		e->_status = READY;

		pair<Node*, entry*> p(*it, e);
		_data.insert(p); //adding each <node,entry> to table

	}
}

TT::~TT() {
}

int TT::getQuantaCurrentStage(Node* n) {
	return (_data.find(n))->second->_quantaCurrentStage;
}

int TT::getQuantaTotal(Node* n) {
	return ((_data.find(n))->second)->_quantaTotal;
}

int TT::getStatus(Node* n) {
	return ((_data.find(n))->second)->_status;
}

int TT::getPriority(Node *n) {
	return ((_data.find(n))->second)->_priority;
}

// adds quanta total & stage count
void TT::IncCurrentStageQuanta(Node* n) {
	((_data.find(n))->second)->_quantaCurrentStage++;
	((_data.find(n))->second)->_quantaTotal++;
}

void TT::setStatus(Node* n, int s) {
	//cout << "TT: setStatus" <<n->getId()<<" to "<< s<< endl;
	((_data.find(n))->second)->_status = s;
	//cout << "TT: Status of " <<n->getId()<<" is "<< ((_data.find(n))->second)->_status<< endl;
}

void TT::setPriority(Node* n, int p) {
	((_data.find(n))->second)->_priority = p;
}

void TT::ResetStage(Node *n) {
	((_data.find(n))->second)->_quantaCurrentStage = 0;
}

void TT::Print() {
	map<Node*, entry*>::iterator it;
	cout << "===printing Thread Table (TT)====" << endl;
	cout << "Id	|status	|Total quanta	|local quanta" << endl;
	for (it = _data.begin(); it != _data.end(); it++) {
		cout << (it->first)->getId() << "	|";

		cout << Status((it->second)->_status) << "	|";

		cout << (it->second)->_quantaTotal << "	|";
		cout << (it->second)->_quantaCurrentStage << "	|" << endl;
	}
	cout << "___________________________________________" << endl;
}

string TT::Status(int i) {
	switch (i) {
	case 0:
		return "READY	";
		break;
	case 1:
		return "RUNNING";
		break;
	case 2:
		return "BLOCKED";
		break;

	default:
		break;
	}
}

